import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:qgchat/common/class/toUserInfo/toUserInfo.dart';
import 'package:qgchat/common/global/global.dart';

/// 头像封装类
/// 有在线与非在线两种样式
/// 当传入token，且本人为非主播时，可跳转到主播详情页
class Avatar extends StatelessWidget {
  Avatar(this.url, this.online, {this.token, Key key}) : super(key: key);

  final String token;
  final String url;
  final bool online;

  // 点击方法
  void clickAction(context) async {
    ToUserInfo.show(context, token);
  }

  // 在线头像样式
  final BoxDecoration onlineStyle = BoxDecoration(
    shape: BoxShape.circle,
    gradient: LinearGradient(
      colors: [col("c571f4"), col("f971ce")],
      begin: FractionalOffset(1, 0),
      end: FractionalOffset(0, 1),
    ),
  );

  // 离线头像样式
  final BoxDecoration offlineStyle = BoxDecoration(
    shape: BoxShape.circle,
    color: col("bfc2cc"),
  );

  // 头像图
  networkImage() {
    return Container(
      width: px(102),
      height: px(102),
      child: CachedNetworkImage(
        imageUrl: "$url",
        fit: BoxFit.cover,
        errorWidget: (context, url, error) {
          return Config.errorNetImage;
        },
      ),
    );
  }

  // 无背景图或者背景图是非线上地址
  final notImage = Container(
    width: px(102),
    height: px(102),
    color: col("edeff2"),
  );

  @override
  Widget build(BuildContext context) {
    return Container(
      width: px(110),
      height: px(110),
      padding: EdgeInsets.all(px(4)),
      decoration: online ? onlineStyle : offlineStyle,
      child: FlatButton(
        onPressed: () => clickAction(context),
        padding: EdgeInsets.zero,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(
            px(55),
          ),
        ),
        child: ClipOval(
          child: (url.isNotEmpty && url.startsWith("http"))
              ? networkImage()
              : notImage,
        ),
      ),
    );
  }
}
